﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ChessGameEngine
{
    public class King : Piece
    {
        public King(ChessEngine chessEngine, object uiElement, PieceColor color)
            : base(chessEngine, uiElement, color)
        {
        }



        public override IEnumerable<TilePosition> GetAllPossibleMoves()
        {
            var position = new TilePosition { I = this.Position.I + 1, J = this.Position.J + 1 };
            if (CheckPosition(position)) yield return position;

            position = new TilePosition { I = this.Position.I - 1, J = this.Position.J +1};
            if (CheckPosition(position)) yield return position;

            position = new TilePosition { I = this.Position.I + 1, J = this.Position.J-1 };
            if (CheckPosition(position)) yield return position;

            position = new TilePosition { I = this.Position.I - 1, J = this.Position.J-1 };
            if (CheckPosition(position)) yield return position;

            position = new TilePosition { I = this.Position.I, J = this.Position.J+1 };
            if (CheckPosition(position)) yield return position;

            position = new TilePosition { I = this.Position.I + 1, J = this.Position.J };
            if (CheckPosition(position)) 
                yield return position;

            var rook = chessEngine.BoardState[7, position.J].Piece;
            if (rook != null && chessEngine.IsOnBoard(position) && chessEngine.IsTileEmpty(position) && !Moved && !rook.Moved) // king side castling
            {
                position = new TilePosition { I = this.Position.I + 2, J = this.Position.J };
                if (chessEngine.IsTileEmpty(position)) 
                    yield return position;
            }

            position = new TilePosition { I = this.Position.I, J = this.Position.J-1 };
            if (CheckPosition(position)) yield return position;


            position = new TilePosition { I = this.Position.I - 1, J = this.Position.J };
            if (CheckPosition(position)) yield return position;
            rook = chessEngine.BoardState[0, position.J].Piece;
            if (rook != null && chessEngine.IsOnBoard(position) && chessEngine.IsTileEmpty(position) && !Moved && !rook.Moved)
            {
                position = new TilePosition { I = this.Position.I - 2, J = this.Position.J };
                if (chessEngine.IsTileEmpty(new TilePosition { I = this.Position.I - 2, J = this.Position.J }) && 
                    chessEngine.IsTileEmpty(new TilePosition { I = this.Position.I - 3, J = this.Position.J }))
                    yield return new TilePosition { I = this.Position.I - 3, J = this.Position.J };
                
            }
        }

        private bool CheckPosition(TilePosition position)
        {
            if (chessEngine.IsOnBoard(position))
            {
                var opponentColor = this.Color == PieceColor.White ? PieceColor.Black : PieceColor.White;
//                ChessBoard board = new ChessBoard(chessEngine.BoardState);
//                bool reachable = board.GetReachablePiecesFor(position, opponentColor).Count() > 0;
//                return  !reachable && chessEngine.IsTileEmpty(position) || chessEngine.IsOpponent(position, this.Color);
                return chessEngine.IsTileEmpty(position) || chessEngine.IsOpponent(position, this.Color);
            }
            return false;
        }
    }
}
